/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.measure;

import org.openaecfm.model.measure.UnitAssignment;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class UnitAssignmentIO extends
		AbstractEntityInstanceInputImpl<UnitAssignment> implements
		IEntityInstanceOutput<UnitAssignment> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(UnitAssignment pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCUNITASSIGNMENT",
				appDataManager.getName(pojo));

		// Set Units
		ListParameter units_pro = new ListParameter();
		for (Object u : pojo.getUnits()) {
			EntityInstanceNameParameter unit_pro = new EntityInstanceNameParameter(
					appDataManager.getName(u));
			units_pro.add(unit_pro);
		}
		ei.addParameter(units_pro);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected UnitAssignment doCreateObject() {
		return new UnitAssignment();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			UnitAssignment pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Units
		ListParameter units_value = (ListParameter) sei.getParameter(0);
		for (Parameter o : units_value) {
			EntityInstanceNameParameter p = (EntityInstanceNameParameter) o;
			pojo.getUnits().add(appDataManager.getObject(p.toInstanceName()));
		}
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			UnitAssignment pojo) {
		return;
	}
}
